//
//  My_threadViewController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/15.
//  Copyright © 2016年 game. All rights reserved.
//

#import "My_threadViewController.h"
#import "SelectionCell.h"
#import "OtherHeadView.h"
#import "SpaceModel.h"
#import "SeleCtContrl.h"
#import "LoginController.h"
#import "thread_listModel.h"
#import "SpacePagerModel.h"
#import "SayHilloViewController.h"
#import "OtherInformationCtrl.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface My_threadViewController ()<UITableViewDelegate,UITableViewDataSource,SelectionCellDeleget>
@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,strong) SpaceModel *model;
@property (nonatomic,strong) NSMutableArray *dataAry;//数组保存数据
@property (nonatomic,strong) NSMutableDictionary *cellHightDic;//缓存cell的属性，高
/** 页码*/
@property (nonatomic, assign) NSInteger nextPage;

@end

@implementation My_threadViewController
-(NSMutableDictionary *)cellHightDic{
    if (!_cellHightDic) {
        _cellHightDic = [NSMutableDictionary dictionary];
    }
    return _cellHightDic;
}
-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

- (IBAction)getBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = [UIScreen mainScreen].bounds;
    self.view.backgroundColor = tabPlainBackColor;
    
    _tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableview.backgroundColor =  tabGropBackColor;
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _tableview.separatorStyle = 0;
    
    [_tableview registerNib:[UINib nibWithNibName:@"SelectionCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableview.showsVerticalScrollIndicator = NO;

    _tableview.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(LoadMorelData)];

    [self.view sendSubviewToBack:_tableview];
    [self.view addSubview:_tableview];
    
    self.nextPage = 2;
    [self AchiveDataWhithPage:1];
    
    __weak typeof(self) weakself = self;
    _tableview.TapBlok = ^{ [weakself AchiveDataWhithPage:1]; };
    
}

#pragma mark---上拉加载
-(void)LoadMorelData{
    
    
    SpacePagerModel *pageModel  = self.model.pager;
    if (self.nextPage > pageModel.total_page.integerValue) {
        [_tableview.footer noticeNoMoreData];
    }else{
        [self AchiveDataWhithPage:self.nextPage];
    }
    self.nextPage ++;
}

-(void)AchiveDataWhithPage:(NSInteger )page{
    
    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:my_thread,(long)page] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
 
        if (!error) {
            self.model = [[SpaceModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
         
            [self.dataAry addObjectsFromArray:self.model.thread_list];
            
            [self.tableview.footer endRefreshing];
            _tableview.isShowImage = YES;
            [_tableview reloadData];
        
        }else{
            _tableview.isShowFile = YES;
            [_tableview reloadData];
            [_tableview.footer noticeNoMoreData];
        }
        
    }];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  [[self.cellHightDic valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] floatValue]+10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.dataAry.count!=0) {
        cell.model = self.dataAry[indexPath.row];
        
        cell.deleget = self;
         [self.cellHightDic setValue:[NSString stringWithFormat:@"%f",cell.height-10] forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    }
    return cell;
}
#pragma mark--SelectionCell中的代理方法
-(void)CommentSelect:(NSString *)tid Row:(NSInteger)row{
    NSString *url = [NSString stringWithFormat:MainSelet,tid];
    
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = tid;
    str.url = url;
    str.isSlide = YES;
    str.rowOfCell = row;
    [self.navigationController pushViewController:str animated:YES];
}

-(void)UserSpaceClick:(NSString *)uid{
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = uid;
    [self.navigationController pushViewController:space animated:YES];
}

-(void)PramisNoLogin{
    LoginController *login = [LoginController new];
    [self.navigationController pushViewController:login animated:YES];
}
-(void)PraimSelect:(NSInteger)section{
    
    thread_listModel *model = self.model.thread_list[section];
    model.isZan = YES;
}

-(void)PraimFile:(NSString *)message{
    [Manager GetHudWhithText:message AndFatherView:self.view];
}

#pragma mark--tableview点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SelectionCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *url = [NSString stringWithFormat:MainSelet,cell.tid];
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = cell.tid;
    str.url = url;
    str.isSlide = NO;
    str.rowOfCell = indexPath.row;
    
    [self.navigationController pushViewController:str animated:YES];
}


@end